package org.example.authservice.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.authservice.mapper.PermissionMapper;
import org.example.authservice.pojo.Permission;
import org.example.authservice.pojo.Result;
import org.example.authservice.service.RoleService;
import org.example.authservice.service.UserService;
import org.example.authservice.vo.RouterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author lj20050120
 */
@RestController
@RequestMapping("/auths")
@Slf4j
public class AuthController {


    private final UserService userService;

    private final RoleService roleService;

    private final PermissionMapper permissionMapper;

    @Autowired
    public AuthController(UserService userService, RoleService roleService, PermissionMapper permissionMapper) {
        this.userService = userService;
        this.roleService = roleService;
        this.permissionMapper = permissionMapper;
    }


    /*@MyLog(content = "登录")*/

    /**
     * 登录
     *
     * @param request 请求参数
     *                username 用户名
     *                password 密码
     * @return Result
     */
    @PostMapping
    public Result<?> getPermission(@RequestBody Map<String, String> request) {
        String username = request.get("username");
        //获取用户权限
        HashSet<Permission> permissionSet = new HashSet<>();
        //1.根据用户id查询出用户拥有的所有role的id
        Integer userId = userService.findIdByUsername(username);
        List<Integer> roles = roleService.getUserRoleById(userId);
        for (Integer roleId : roles) {
            //2.根据role的id查询出对应拥有的权限id
            List<Integer> permissionRoles = roleService.getPermissionRoleById(roleId);
            for (Integer permissionId : permissionRoles) {
                //3.根据权限id查询对应权限
                Permission permission = permissionMapper.selectById(permissionId);
                permissionSet.add(permission);
            }
        }
        System.out.println(permissionSet);
        List<RouterVO> routerVos = getRouterVos(permissionSet);
        //循环一级路由，获取所有子节点
        for (RouterVO routerVO : routerVos) {
            List<RouterVO> childrenList = builderTree(permissionSet, routerVO.getId());
            routerVO.setChildren(childrenList);
        }
        return Result.success(routerVos);
    }

    private static List<RouterVO> getRouterVos(HashSet<Permission> permissionSet) {
        List<RouterVO> routerVos = new ArrayList<>();
        //获取所有的一级路由
        for (Permission permission : permissionSet) {
            if (permission.getParentId() == 0) {
                RouterVO routerVO = new RouterVO();
                routerVO.setPath(permission.getPath());
                routerVO.setIcon(permission.getIcon());
                routerVO.setName(permission.getName());
                routerVO.setId(permission.getId());
                routerVO.setParentId(permission.getParentId());
                routerVO.setComment(permission.getComment());
                routerVO.setType(permission.getType());
                routerVos.add(routerVO);
            }
        }
        return routerVos;
    }


    /**
     *  构建树形结构
     * @param permissions 权限集合
     * @param parentId 父节点id
     * @return 子节点集合
     */
    private List<RouterVO> builderTree(Set<Permission> permissions, Integer parentId) {
        List<RouterVO> childrenList = new ArrayList<>();
        for (Permission permission : permissions) {
            //判断id是否与传进来的parentId相同
            if (permission.getParentId().equals(parentId)) {
                RouterVO routerVO = new RouterVO();
                routerVO.setPath(permission.getPath());
                routerVO.setIcon(permission.getIcon());
                routerVO.setName(permission.getName());
                routerVO.setId(permission.getId());
                routerVO.setParentId(permission.getParentId());
                routerVO.setType(permission.getType());
                routerVO.setComment(permission.getComment());
                childrenList.add(routerVO);
            }
        }
        return childrenList;
    }
}